/* *-----------------------------------------------------------------------
 *   Original by: http://thiscouldbebetter.wordpress.com/2011/07/04/pausing-an-mp3-file-using-jlayer/
 *   Last modified: 21-jul-2012 by Arthur Assuncao 
 *----------------------------------------------------------------------
 */
/*
 * This is the class that should be used or edited for creating new objects
 * to be played paused and stopped.
 */
package cs341.cs.uwosh.edu.jBeats;
//import javazoom.jl.decoder.JavaLayerException;


class SoundJLayer implements Runnable{
        public boolean isPlaying = false;
        public boolean isPaused = false;
        public boolean isStopped = true;
    
        private String filePath;
        private JLayerPlayerPausable player;
        private Thread playerThread;
        private String namePlayerThread = "AudioPlayerThread";
        private PlaybackListener playbackListener = new PlaybackListener();
        int finalFrameS = 0;
        
        public SoundJLayer(String filePath){
                this.filePath = filePath;
//                finalFrameS = player.finalFrame;
        }
        
        public SoundJLayer(String filePath, String namePlayerThread){
                this.filePath = filePath;
                this.namePlayerThread = namePlayerThread;
                finalFrameS = player.finalFrame;
                
        }

        //Starts a new thread and starts to play the song, or resumes paused
        public void play(){
                if (this.player == null){
                        this.playerInitialize();
                }
                else if(!this.player.isPaused() || this.player.isComplete() || this.player.isStopped()){
                        this.stop();
                        this.playerInitialize();
                }
                this.playerThread = new Thread(this, namePlayerThread);
                this.playerThread.setDaemon(true);
                this.playerThread.start();
                isPlaying = true;
                isPaused = false;
                isStopped = false;
        }

        //Pauses the thread to resume music playing after music started
        public void pause(){
                if (this.player != null){
                        this.player.pause();

                        if(this.playerThread != null){
                                //this.playerThread.stop(); //unsafe method
                                this.playerThread = null;
                        }
                }
                isPlaying = false;
                isPaused = true;
                isStopped = false;
        }

        public void pauseToggle(){
                if (this.player != null){
                        if (this.player.isPaused() && !this.player.isStopped()){
                                this.play();
                        }
                        else{
                                this.pause();
                        }
                }
        }

        public void stop(){
                if (this.player != null){
                        this.player.stop();

                        if(this.playerThread != null){
                                //this.playerThread.stop(); //unsafe method
                                this.playerThread = null;
                        }
                }
                isPlaying = false;
                isPaused = false;
                isStopped = true;
        }

        private void playerInitialize(){
                //try {
                        this.player = new JLayerPlayerPausable(this.filePath);
                        this.player.setPlaybackListener(this.playbackListener);
                //}
                //catch (JavaLayerException e) {
                        //e.printStackTrace();
                //}
        }

        // IRunnable members
        public void run(){
                try{
                        this.player.resume();
                }
                catch (javazoom.jl.decoder.JavaLayerException ex){
                        ex.printStackTrace();
                }
        }
        
        public boolean getPlaying(){
            return isPlaying;
        }
        
        public boolean getPaused(){
            return isPaused;
        }
        
        public boolean getStopped(){
            return isStopped;
        }
                
        public int getIndexFinal(){
            return finalFrameS;
        }
        private static class PlaybackListener extends JLayerPlayerPausable.PlaybackAdapter {
                // PlaybackListener members
                @Override
                public void playbackStarted(JLayerPlayerPausable.PlaybackEvent playbackEvent){
                        System.err.println("PlaybackStarted()");
                }
                
                @Override
                public void playbackPaused(JLayerPlayerPausable.PlaybackEvent playbackEvent){
                        System.err.println("PlaybackPaused()");
                }

                @Override
                public void playbackFinished(JLayerPlayerPausable.PlaybackEvent playbackEvent){
                        System.err.println("PlaybackStopped()");
                }
        }
}